﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ToolDumpData
{
    public partial class frmDumpData : Form
    {
        Mortgage.Srv.IAgentSrv svcAgent = Mortgage.Srv.SrvFactory.CreateAgentSrv();

        public frmDumpData()
        {
            InitializeComponent();
        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void frmDumpData_Load(object sender, EventArgs e)
        {
            drpAgent.DataSource = svcAgent.GetAll();
            drpAgent.DisplayMember = "Name";
            drpAgent.ValueMember = "ID";

        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            int agentId = Convert.ToInt32(drpAgent.SelectedValue);
            int k = Convert.ToInt32(txtNumMor.Text);
            int count = Convert.ToInt32(txtNumMor.Text) * dteEnd.Value.Subtract(dteStart.Value).Days;
            progressBar1.Maximum = count;
            string[] name = txtCustomerName.Text.Split(',');
            string[] itemName = txtItemName.Text.Split(',');
            int idx;
            int numDayMor = Convert.ToInt32(txtNumDay.Text);
            Random rndnam = new Random();
            string itName, cusName;
            DateTime st  = dteStart.Value;
            int monSt, monEn;
            monSt = Convert.ToInt32( txtMoneyBegin.Text);
            monEn = Convert.ToInt32( txtMoneyEnd.Text);

            progressBar1.Maximum = count;
            
            for (int i = 0; i < count; i++)
            {
                idx = rndnam.Next(name.Length - 1);
                cusName = name[idx];

                idx = rndnam.Next(itemName.Length - 1);
                itName = itemName[idx];

                Mortgage.DAL.Entity.tMortgage m = new Mortgage.DAL.Entity.tMortgage();
                
                m.Age_ID = agentId;
                m.ItemName = itName;
                m.CustomerName = cusName;
                m.MorDate = st;
                m.MorMoney = rndnam.Next(monSt, monEn);
                m.NumDayExpire = rndnam.Next(numDayMor);
                
                m.CreatedDate = st;
                m.ModifiedDate = st;

                m.CreatedBy = "Auto";
                m.ModifiedBy = "Auto";
                m.SerialNo = i.ToString();

                Mortgage.Srv.IMortgageSrv svcMort = Mortgage.Srv.SrvFactory.CreateMortgageSrv();

                svcMort.Insert(m);

                Mortgage.Srv.IBillSrv svcBill = Mortgage.Srv.SrvFactory.CreateBillSrv();
                 Mortgage.DAL.Entity.tBill b = new Mortgage.DAL.Entity.tBill();
               
                b.Age_ID = agentId;
          
                b.BillDate = st.AddDays( rndnam.Next( 100 ));
                b.MoneyAmount = m.MorMoney + rndnam.Next ( 10000000);
                b.BillType = -2;
                
                b.CreatedDate = st;
                b.ModifiedDate = st;
                
                b.CreatedBy = "Auto";
                b.ModifiedBy = "Auto";

                b.MorID = m.ID;
                svcBill.Insert(b, Mortgage.Srv.Cls.EnmMortgageStatus.Done);

                if ( i % k == 0 )
                    st = st.AddDays(1);

               
                progressBar1.Increment(i);
            }
            MessageBox.Show("Done" + count + "recodes!");


        }
    }
}
